package com.tebiecloud.mpopen.server.controller.feign;

import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.mpopen.client.model.mpopen.AccessToken;
import com.tebiecloud.mpopen.client.service.IMpopenApiServiceClient;
import com.tebiecloud.mpopen.server.service.MpopenApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 第三方平台接口
 * @Author: tebie
 * @Date: 2019-08-09 11:05
 */
@Slf4j
@RestController
@RequestMapping("/feign/api")
public class ApiController implements IMpopenApiServiceClient {

    @Autowired
    private MpopenApiService mpopenApiService;

    /**
     * 获取接口调用凭据
     * @return
     */
    @PostMapping("infoWxapiAccessToken")
    @Override
    public ResultBody<String> getWxapiAccessToken(@RequestParam("appid") String appid,
                                                  @RequestParam("force") boolean force) {
        String accessToken = mpopenApiService.getWxapiAccessToken(appid, force);

        if(StringUtils.isEmpty(accessToken)) {
            log.info("xiaoc>> 获取微信公众号access_token失败, appid: {}", appid);
            return ResultBodyUtils.failed("获取微信公众号access_token失败");
        }

        return ResultBodyUtils.ok(accessToken);
    }

    /**
     * 网页授权，通过 code 换取 access_token
     * @param appid
     * @param code
     * @return
     */
    @PostMapping("infoAccessToken")
    @Override
    public ResultBody<AccessToken> getAccessToken(@RequestParam("appid") String appid, @RequestParam("code") String code) {
        AccessToken accessToken = mpopenApiService.getAccessToken(appid, code);
        return ResultBodyUtils.ok(accessToken);
    }

}
